package cc.mrbird.febs.suggest.controller;

import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.suggest.domain.BusBean;
import cc.mrbird.febs.suggest.domain.SuggestBean;
import cc.mrbird.febs.suggest.service.SuggestService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("suggest")
public class SuggestController extends BaseController {
    @Autowired
    SuggestService suggestService;

    private String message;

    @PostMapping
    public void addSuggest(SuggestBean suggestBean) throws FebsException {
        try {
            suggestBean.setCreateTime(new Date());
            suggestBean.setReplayContent("");
            this.suggestService.save(suggestBean);
        } catch (Exception e) {
            message = "新增失败";
            System.out.println(e);
            throw new FebsException(message);
        }
    }

    @PutMapping
    public void updateSuggest(SuggestBean suggestBean) throws FebsException {
        try {
            suggestBean.setReplayTime(new Date());
            this.suggestService.saveOrUpdate(suggestBean);
        } catch (Exception e) {
            message = "修改失败";
            throw new FebsException(message);
        }
    }

    @DeleteMapping("/{suggestIds}")
    public void deleteSuggests(@NotBlank(message = "{required}") @PathVariable String suggestIds) throws FebsException {
        try {
            String[] ids = suggestIds.split(StringPool.COMMA);
            this.suggestService.removeByIds(Arrays.asList(ids));
        } catch (Exception e) {
            message = "删除失败";
            throw new FebsException(message);
        }
    }

    @GetMapping("list")
    public Map<String, Object> SuggestList(QueryRequest queryRequest, SuggestBean suggestBean) {
        return getDataTable(suggestService.findSuggestDetail(suggestBean, queryRequest));
    }

    @GetMapping("all")
    public Map<String, Object> SuggestListAll(QueryRequest queryRequest, SuggestBean suggestBean) {
        return getDataTable(suggestService.findSuggestAll(suggestBean, queryRequest));
    }

    @GetMapping("listBusHour")
    public List<BusBean> listBusHour(){
        return suggestService.listBusHour();
    }

    @GetMapping("listBusTopN")
    public List<BusBean> listBusTopN(){
        return suggestService.listBusTopN();
    }
    @GetMapping("listStation")
    public List<BusBean> listStation(){
        return suggestService.listStation();
    }
    @GetMapping("listBusNum")
    public List<BusBean> listBusNum(){
        return suggestService.listBusNum();
    }
    @GetMapping("listStationTopN")
    public List<BusBean> listStationTopN(){
        return suggestService.listStationTopN();
    }
}
